package day01;

import java.util.Stack;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/12 12:22
 * 力扣 150. 逆波兰表达式求值
 */
public class StackPractice02 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String t : tokens) {
            switch (t) {
                case "+": {
                    int i = stack.pop();
                    int j = stack.pop();
                    stack.push(j + i);
                    break;
                }
                case "-": {
                    int i = stack.pop();
                    int j = stack.pop();
                    stack.push(j - i);
                    break;
                }
                case "*": {
                    int i = stack.pop();
                    int j = stack.pop();
                    stack.push(j * i);
                    break;
                }
                case "/": {
                    int i = stack.pop();
                    int j = stack.pop();
                    stack.push(j / i);
                    break;
                }
                default: {
                    stack.push(Integer.valueOf(t));
                }
            }
        }
        return stack.peek();
    }
}
